package com.ibm.moa.outmail.utils;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.webkit.MimeTypeMap;
import android.widget.Toast;

import com.ibm.moa.outmail.R;

public class OpenFiles {
	public static void startFile(Context context, File currentPath){
		if(currentPath!=null&&currentPath.isFile())
        {
            String fileName = currentPath.toString();
            Intent intent;
            if(checkEndsWithInStringArray(fileName, context.getResources().getStringArray(R.array.fileEndingImage))){
                intent = OpenFiles.getImageFileIntent(currentPath);
                context.startActivity(intent);
            }else if(checkEndsWithInStringArray(fileName, context.getResources().
                    getStringArray(R.array.fileEndingWebText))){
                intent = OpenFiles.getHtmlFileIntent(currentPath);
                context.startActivity(intent);
            }else if(checkEndsWithInStringArray(fileName, context.getResources().
                    getStringArray(R.array.fileEndingAudio))){
                intent = OpenFiles.getAudioFileIntent(currentPath);
                context.startActivity(intent);
            }else if(checkEndsWithInStringArray(fileName, context.getResources().
                    getStringArray(R.array.fileEndingVideo))){
                intent = OpenFiles.getVideoFileIntent(currentPath);
                context.startActivity(intent);
            }else if(checkEndsWithInStringArray(fileName, context.getResources().
                    getStringArray(R.array.fileEndingText))){
                intent = OpenFiles.getTextFileIntent(currentPath);
                context.startActivity(intent);
            }else if(checkEndsWithInStringArray(fileName, context.getResources().
                    getStringArray(R.array.fileEndingPdf))){
                intent = OpenFiles.getPdfFileIntent(currentPath);
                context.startActivity(intent);
            }else if(checkEndsWithInStringArray(fileName, context.getResources().
                    getStringArray(R.array.fileEndingWord))){
                intent = OpenFiles.getWordFileIntent(currentPath);
                context.startActivity(intent);
            }else if(checkEndsWithInStringArray(fileName, context.getResources().
                    getStringArray(R.array.fileEndingExcel))){
                intent = OpenFiles.getExcelFileIntent(currentPath);
                context.startActivity(intent);
            }else if(checkEndsWithInStringArray(fileName, context.getResources().
                    getStringArray(R.array.fileEndingPPT))){
                intent = OpenFiles.getPPTFileIntent(currentPath);
                context.startActivity(intent);
            }else if(checkEndsWithInStringArray(fileName, context.getResources().
                    getStringArray(R.array.fileEndingZip))){
                intent = OpenFiles.getZipFileIntent(currentPath);
                context.startActivity(intent);
            }else if(checkEndsWithInStringArray(fileName, context.getResources().
                    getStringArray(R.array.fileEndingRar))){
                intent = OpenFiles.getRarFileIntent(currentPath);
                context.startActivity(intent);
            }else if(checkEndsWithInStringArray(fileName, context.getResources().
                    getStringArray(R.array.fileEndingWps))){
                intent = OpenFiles.getWpsFileIntent(currentPath);
                context.startActivity(intent);
            }else if(checkEndsWithInStringArray(fileName, context.getResources().
                    getStringArray(R.array.fileEndingDps))){
                intent = OpenFiles.getDpsFileIntent(currentPath);
                context.startActivity(intent);
            }else
            {
            	openFile(context, currentPath.getAbsolutePath());
            }
        }else
        {
        	throw new RuntimeException("不是文件");
        }
	}
	
	 private static boolean checkEndsWithInStringArray(String checkItsEnd,
             String[] fileEndings){
		 for(String aEnd : fileEndings){
		     if(checkItsEnd.toLowerCase().endsWith(aEnd.toLowerCase()))
		         return true;
		 }
		 return false;
	}

	public static Intent getWpsFileIntent(File file)
    {
   	 Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/wps");
        return intent;
    }
	public static Intent getDpsFileIntent(File file)
    {
   	 Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/dps");
        return intent;
    }
     public static Intent getZipFileIntent(File file)
     {
    	 Intent intent = new Intent("android.intent.action.VIEW");
         intent.addCategory("android.intent.category.DEFAULT");
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         Uri uri = Uri.fromFile(file);
         intent.setDataAndType(uri, "application/zip");
         return intent;
     }
     public static Intent getRarFileIntent(File file)
     {
    	 Intent intent = new Intent("android.intent.action.VIEW");
         intent.addCategory("android.intent.category.DEFAULT");
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         Uri uri = Uri.fromFile(file);
         intent.setDataAndType(uri, "application/x-rar-compressed");
         return intent;
     }
    //android获取一个用于打开HTML文件的intent
      public static Intent getHtmlFileIntent(File file)
      {
          Uri uri = Uri.parse(file.toString()).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(file.toString()).build();
          Intent intent = new Intent("android.intent.action.VIEW");
          intent.setDataAndType(uri, "text/html");
          return intent;
      }
    //android获取一个用于打开图片文件的intent
      public static Intent getImageFileIntent(File file)
      {
          Intent intent = new Intent("android.intent.action.VIEW");
          intent.addCategory("android.intent.category.DEFAULT");
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          Uri uri = Uri.fromFile(file);
          intent.setDataAndType(uri, "image/*");
          return intent;
      }
      //android获取一个用于打开PDF文件的intent
      public static Intent getPdfFileIntent(File file)
      {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/pdf");
        return intent;
      }
    //android获取一个用于打开文本文件的intent
    public static Intent getTextFileIntent(File file)
    {   
      Intent intent = new Intent("android.intent.action.VIEW");
      intent.addCategory("android.intent.category.DEFAULT");
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      Uri uri = Uri.fromFile(file);
      intent.setDataAndType(uri, "text/plain");
      return intent;
    }
  
    //android获取一个用于打开音频文件的intent
      public static Intent getAudioFileIntent(File file)
      {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("oneshot", 0);
        intent.putExtra("configchange", 0);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "audio/*");
        return intent;
      }
      //android获取一个用于打开视频文件的intent
      public static Intent getVideoFileIntent(File file)
      {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("oneshot", 0);
        intent.putExtra("configchange", 0);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "video/*");
        return intent;
      }
  
  
      //android获取一个用于打开CHM文件的intent
      public static Intent getChmFileIntent(File file)
      {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/x-chm");
        return intent;
      }
  
  
    //android获取一个用于打开Word文件的intent
      public static Intent getWordFileIntent(File file)
      {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/msword");
        return intent;
      }
    //android获取一个用于打开Excel文件的intent
      public static Intent getExcelFileIntent(File file)
      {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/vnd.ms-excel");
        return intent;
      }
    //android获取一个用于打开PPT文件的intent
      public static Intent getPPTFileIntent(File file)
      {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
        return intent;
      }
      /**
  	 * 调用系统方法打开指定路径的文件
  	 * 
  	 * @param context
  	 * @param filePath
  	 */
  	public static void openFile(Context context, String filePath) {
  		if (filePath != null && !"".equals(filePath)) {
  			try {
  				context.startActivity(getAllIntent(filePath));
  			} catch (Exception e) {
  				Toast.makeText(context, "无法打开此文件！", Toast.LENGTH_SHORT).show();
  			}
  		} else {
  			// Toast.makeText(context, "无法打开此文件！", Toast.LENGTH_SHORT).show();
  		}
  	}

  	public static Intent getAllIntent(String param) {
  		Intent intent = new Intent();
  		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  		intent.setAction(android.content.Intent.ACTION_VIEW);
  		Uri uri = Uri.fromFile(new File(param));
  		intent.setDataAndType(uri, getMimeType(param));
  		return intent;
  	}

  	/**
  	 * 得到指定文件的MIME-type
  	 * 
  	 * @param file
  	 * @return
  	 */
  	public static String getMimeType(File file) {
  		MimeTypeMap map = MimeTypeMap.getSingleton();
  		String fileName = file.getAbsolutePath();
  		int index = fileName.lastIndexOf(".");
  		if (index < 0) {
  			return null;
  		}
  		return map.getMimeTypeFromExtension(fileName.substring(index + 1));
  	}

  	/**
  	 * 得到指定文件的MIME-type
  	 * 
  	 * @param fileName
  	 * @return
  	 */
  	public static String getMimeType(String fileName) {
  		MimeTypeMap map = MimeTypeMap.getSingleton();
  		int index = fileName.lastIndexOf(".");
  		if (index < 0) {
  			return null;
  		}
  		return map.getMimeTypeFromExtension(fileName.substring(index + 1));
  	}
     
}

